package top.yxf.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yxf.common.utils.PageUtils;
import top.yxf.common.utils.Query;

import top.yxf.gulimall.product.dao.CategoryDao;
import top.yxf.gulimall.product.entity.CategoryEntity;
import top.yxf.gulimall.product.service.CategoryBrandRelationService;
import top.yxf.gulimall.product.service.CategoryService;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        categoryEntities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity,categoryEntities));
            return categoryEntity;
        }).sorted((menu1,menu2) -> {
            return ((menu1.getSort() == null ? 0 : menu1.getSort()) - ((menu2.getSort() == null ? 0 : menu2.getSort())));
        }).collect(Collectors.toList());
        return categoryEntities;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCateGoryPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();

        List<Long> parentPath = findCateGoryPath(catelogId, paths);
        // 反转list中的值
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);

    }

    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);

        categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
    }

    private List<Long> findCateGoryPath(Long categoryId,List<Long> paths) {
        paths.add(categoryId);
        CategoryEntity byId = this.getById(categoryId);
        if (byId.getParentCid() != 0) {
            findCateGoryPath(byId.getParentCid(), paths);
        }
        return paths;

    }

    public List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all) {

        List<CategoryEntity> childrenList = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            return ((menu1.getSort() == null ? 0 : menu1.getSort()) - ((menu2.getSort() == null ? 0 : menu2.getSort())));
        }).collect(Collectors.toList());
        return childrenList;
    }

}